#include "dwgarc.h"
#include <QtMath>

DwgArc::DwgArc(QObject *parent)
    : DwgObject{parent},
      mStartAngle(0),
      mSpanAngle(90)  // 默认角度
{
}

void DwgArc::SetData(QPointF pt1, QPointF pt2)
{
    mCenPt = pt1;
    mRad = qSqrt((pt2.x() - pt1.x()) * (pt2.x() - pt1.x()) +
           (pt2.y() - pt1.y()) * (pt2.y() - pt1.y()));
}

void DwgArc::SetAngle(qreal startAngle, qreal spanAngle) // 30-120
{
    mStartAngle = startAngle;
    mSpanAngle = spanAngle;
}

void DwgArc::Draw(QPainter *painter)
{
    //=========抑制线宽变化========//
    QPen pen;
    pen.setCosmetic(true);       // 允许线宽缩放
    pen.setWidth(1);             // 使用逻辑线宽
    painter->setPen(pen);
    //==========选择+删除=========//
    if(mDeleteed) return;
    if(mSelected)
    {
        painter->setPen(Qt::red);
    }

    QRectF rect(mCenPt.x() - mRad, mCenPt.y() - mRad, 2 * mRad, 2 * mRad);   
    painter->drawArc(rect, mStartAngle * 16, mSpanAngle * 16);  // Qt中角度单位为1/16度

    painter->setPen(Qt::black);
}

//==选择+删除==//
void DwgArc::Select(QPointF pt)
{
    qreal dist = sqrt((pt.x()-mCenPt.x())*(pt.x()-mCenPt.x())
                      +(pt.y()-mCenPt.y())*(pt.y()-mCenPt.y()));
    if(fabs(dist-mRad) < 3)
        mSelected = true;
}
